package security;

import javax.swing.*;

/**
 * 
 * @author Keith Mattix
 */
public class CeaserCipher extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Creates new form CeaserCipher */
	public CeaserCipher() {
		initComponents();
	}

	private void initComponents() {

		jScrollPane1 = new JScrollPane();
		jTextArea1 = new JTextArea();
		jScrollPane2 = new JScrollPane();
		jTextArea2 = new JTextArea();
		jButton1 = new JButton();
		jButton2 = new JButton();
		jTextField1 = new JTextField();
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setTitle("Ceaser Cipher Encryptor and Decoder");

		jTextArea1.setColumns(20);
		jTextArea1.setRows(5);
		jScrollPane1.setViewportView(jTextArea1);

		jTextArea2.setColumns(20);
		jTextArea2.setRows(5);
		jScrollPane2.setViewportView(jTextArea2);

		jButton1.setText("Encrypt");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jButton2.setText("Decode");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jTextField1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jTextField1ActionPerformed(evt);
			}
		});

		jLabel1.setText("Shift:*");

		jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
		jLabel2.setText("*If left blank, the default value,3, will be used ");

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(
						GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addGap(58, 58, 58)
								.addComponent(jButton1)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED,
										196, Short.MAX_VALUE)
								.addComponent(jButton2).addGap(64, 64, 64))
				.addGroup(
						GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(97, Short.MAX_VALUE)
								.addComponent(jLabel2).addGap(101, 101, 101))
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.TRAILING)
												.addGroup(
														layout.createSequentialGroup()
																.addContainerGap()
																.addComponent(
																		jScrollPane1,
																		GroupLayout.DEFAULT_SIZE,
																		203,
																		Short.MAX_VALUE))
												.addGroup(
														GroupLayout.Alignment.LEADING,
														layout.createSequentialGroup()
																.addGap(181,
																		181,
																		181)
																.addComponent(
																		jLabel1)))
								.addGroup(
										layout.createParallelGroup(
												GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addPreferredGap(
																		LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		jTextField1,
																		GroupLayout.DEFAULT_SIZE,
																		27,
																		Short.MAX_VALUE)
																.addGap(210,
																		210,
																		210))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(38, 38,
																		38)
																.addComponent(
																		jScrollPane2,
																		GroupLayout.PREFERRED_SIZE,
																		195,
																		GroupLayout.PREFERRED_SIZE)
																.addContainerGap()))));
		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.LEADING, false)
										.addComponent(jScrollPane2)
										.addComponent(jScrollPane1,
												GroupLayout.DEFAULT_SIZE, 234,
												Short.MAX_VALUE))
						.addGap(18, 34, Short.MAX_VALUE)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.LEADING)
										.addComponent(jButton2)
										.addComponent(jButton1))
						.addGap(38, 38, 38)
						.addGroup(
								layout.createParallelGroup(
										GroupLayout.Alignment.BASELINE)
										.addComponent(jLabel1)
										.addComponent(jTextField1,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addGap(18, 18, 18).addComponent(jLabel2)
						.addContainerGap()));

		pack();
	}

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton2ActionPerformed
		jTextArea1.setText(decode(jTextArea2.getText(), jTextField1.getText()));
	}

	private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jTextField1ActionPerformed
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		jTextArea2
				.setText(encrypt(jTextArea1.getText(), jTextField1.getText()));
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main() {
		java.awt.EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				new CeaserCipher().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JButton jButton1;
	private JButton jButton2;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane2;
	private JTextArea jTextArea1;
	private JTextArea jTextArea2;
	private JTextField jTextField1;

	// End of variables declaration//GEN-END:variables

	private String encrypt(String text, String shift) {
		int shiftInt;
		if (shift.equals("")) {
			shiftInt = 3;

		} else {
			shiftInt = Integer.parseInt(shift);
		}
		String encryptedMessage = new String();
		text = text.toUpperCase();
		for (int i = 0; i < text.length(); i++) {
			char ch = text.charAt(i);
			if (ch >= 'A' && ch <= 'Z') {
				int oldPositionInAlphabet = ch - 'A';
				int newPositionInAlphabet = (oldPositionInAlphabet + shiftInt) % 26;
				encryptedMessage = encryptedMessage
						+ (char) (newPositionInAlphabet + 'A');
			}
		}
		return encryptedMessage;
	}

	private String decode(String text, String shift) {
		int shiftInt;
		if (shift.equals("")) {
			shiftInt = 3;
		} else {
			shiftInt = Integer.parseInt(shift);
		}
		String decodedMessage = new String();
		text = text.toUpperCase();
		for (int i = 0; i < text.length(); i++) {
			char ch = text.charAt(i);
			if (ch >= 'A' && ch <= 'Z') {
				int encodedPosition = ch + 'A';
				int decodedPosition = (encodedPosition - shiftInt) % 26;
				decodedMessage = decodedMessage
						+ (char) (decodedPosition + 'A');
			}
		}
		return decodedMessage;

	}
}